package com.wangrui.us3.cos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.Headers;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.model.GeneratePresignedUrlRequest;
import com.qcloud.cos.model.ResponseHeaderOverrides;
import com.qcloud.cos.utils.DateUtils;

import java.net.URL;
import java.util.Date;

public class SignedUrlTest {
    public static void main(String[] args) {
        COSClient cosClient = CosUtil.getClient();
        // 存储桶的命名格式为 BucketName-APPID，此处填写的存储桶名称必须为此格式
        String bucketName = "bucket2-1251548693";
        // 对象键(Key)是对象在存储桶中的唯一标识。详情请参见 [对象键](https://cloud.tencent.com/document/product/436/13324)
        String key = "test/sm/17592232724392.mp4";
        System.out.println(cosClient.getObjectUrl(bucketName, key));
        GeneratePresignedUrlRequest req =
                new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
        // 设置下载时返回的 http 头
        ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
        String responseContentType = "video/mp4";
        String responseContentLanguage = "zh-CN";
        // 设置返回头部里包含文件名信息
        String responseContentDispositon = "filename=17592232724392.mp4";
        String responseCacheControl = "no-cache";
        String cacheExpireStr =
                DateUtils.formatRFC822Date(new Date(System.currentTimeMillis() + 24L * 3600L * 1000L));
        responseHeaders.setContentType(responseContentType);
        responseHeaders.setContentLanguage(responseContentLanguage);
        responseHeaders.setContentDisposition(responseContentDispositon);
        responseHeaders.setCacheControl(responseCacheControl);
        responseHeaders.setExpires(cacheExpireStr);
        req.setResponseHeaders(responseHeaders);
        // 设置签名过期时间(可选)，若未进行设置，则默认使用 ClientConfig 中的签名过期时间(1小时)
        // 这里设置签名在半个小时后过期
        Date expirationDate = new Date(System.currentTimeMillis() + 30L * 60L * 1000L);
        req.setExpiration(expirationDate);
        // 填写本次请求的参数
        req.addRequestParameter("param1", "value1");
        // 填写本次请求的头部
        // host 必填
        req.putCustomRequestHeader(Headers.HOST, cosClient.getClientConfig().getEndpointBuilder().buildGeneralApiEndpoint(bucketName));
        req.putCustomRequestHeader("header1", "value1");
        URL url = cosClient.generatePresignedUrl(req);
        System.out.println(url.toString());
// 确认本进程不再使用 cosClient 实例之后，关闭即可
        cosClient.shutdown();
    }
}
